// 334.递增的三元子序列
package Leetcode;

public class Solution334 {
    public boolean increasingTriplet(int[] nums) {
        if (nums.length < 3)
            return false;

        // 数组左边的最小值
        int[] leftMin = new int[nums.length];
        leftMin[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            leftMin[i] = Math.min(leftMin[i - 1], nums[i]);
        }

        // 数组右边的最大值
        int[] rightMax = new int[nums.length];
        rightMax[nums.length - 1] = nums[nums.length - 1];
        for (int i = nums.length - 2; i >= 0; i--) {
            rightMax[i] = Math.max(rightMax[i + 1], nums[i]);
        }

        // 遍历数组寻找中间值
        for (int i = 1; i < nums.length - 1; i++) {
            if (nums[i] > leftMin[i - 1] && nums[i] < rightMax[i + 1])
                return true;
        }
        return false;
    }

    public boolean increasingTriplet2(int[] nums) {
        if (nums.length < 3)
            return false;
        int first = nums[0];
        int second = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > second)
                return true;
            else if (nums[i] < first)
                first = nums[i];
            else
                second = nums[i];
        }
        return false;
    }
}
